<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="景点名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入景点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="景点位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入景点位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="景点状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择景点状态" clearable>
          <el-option
            v-for="dict in spot_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:spot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:spot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:spot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:spot:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="景点名称" align="center" prop="name" />
      <!-- <el-table-column label="景点描述" align="center" prop="description" :show-overflow-tooltip="true" />       -->
      <el-table-column label="景点位置" align="center" prop="location" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="联系方式" align="center" prop="contact" />
      <el-table-column label="景点类型" align="center" prop="spotType" >
        <template #default="scope">
          <dict-tag :options="spot_type" :value="scope.row.spotType"/>
        </template>
      </el-table-column>
      <el-table-column label="景点状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="spot_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:spot:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:spot:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改景点管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="spotRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="景点名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入景点名称" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <!-- <el-form-item label="景点位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入景点位置" />
        </el-form-item> -->
        <el-row>
          <el-col :span="8">
            <el-form-item label="所属区域" prop="province">
              <!-- <el-input v-model="form.province" placeholder="请输入所属省份" /> -->
              <el-select v-model="form.province" placeholder="请选择所属省份" clearable @change="getAreaList(2, form.province);"> 
                <el-option v-for="dict in provinceList" :key="dict.id" :label="dict.name" :value="dict.id" /> 
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item prop="city" label-width="0">
              <el-select v-model="form.city" placeholder="请选择所属地市" clearable @change="getAreaList(3, form.city);"> 
                <el-option v-for="dict in cityList" :key="dict.id" :label="dict.name" :value="dict.id" /> 
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7"  :offset="1">
            <el-form-item prop="district" label-width="0">
              <el-select v-model="form.district" placeholder="请选择所属区县" clearable > 
                <el-option v-for="dict in districtList" :key="dict.id" :label="dict.name" :value="dict.id" /> 
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="门票价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入门票价格" />
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="2">
            <el-form-item label="联系方式" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系方式" />
            </el-form-item> 
          </el-col>
        </el-row>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入详细地址" />
        </el-form-item>
        
        <el-form-item label="景点描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-row>
          <el-col :span="11">
            <el-form-item label="开放时间" prop="openingHours">
              <el-time-picker
                v-model="form.openingHours"
                value-format="hh:mm"
                placeholder="开放时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="2">
            <el-form-item label="关闭时间" prop="closingHours">
              <el-time-picker
                v-model="form.closingHours"
                placeholder="关闭时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="景点状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in spot_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="景点图片" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入景点图片URL" />
        </el-form-item>

        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Spot">
import { listSpot, getSpot, delSpot, addSpot, updateSpot } from "@/api/manage/spot";

import { queryByLevelAndId} from "@/api/manage/area";


const value1 = ref<[Date, Date]>([
  new Date(2016, 9, 10, 8, 40),
  new Date(2016, 9, 10, 9, 40),
])
const { proxy } = getCurrentInstance();
const { spot_status } = proxy.useDict('spot_status');
const { spot_type } = proxy.useDict('spot_type');

const spotList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    description: null,
    location: null,
    spotType: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询景点管理列表 */
function getList() {
  loading.value = true;
  listSpot(queryParams.value).then(response => {
    spotList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    description: null,
    location: null,
    province: null,
    city: null,
    district: null,
    address: null,
    contact: null,
    spotType: null,
    openingHours: null,
    closingHours: null,
    price: null,
    status: null,
    imageUrl: null,
    createdBy: null,
    updatedBy: null,
    createdAt: null,
    updatedAt: null
  };
  proxy.resetForm("spotRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加景点管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSpot(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改景点管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["spotRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSpot(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSpot(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除景点管理编号为"' + _ids + '"的数据项？').then(function() {
    return delSpot(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/spot/export', {
    ...queryParams.value
  }, `spot_${new Date().getTime()}.xlsx`)
}


//查詢地区
const provinceList = ref([]);
const cityList = ref([]);
const districtList = ref([]);


function getAreaList(level, parentId) {
  const areaParams = reactive({
    level: level, // 1: 省份, 2: 城市, 3: 区县
    parentId: parentId, // 上级地区的 ID, 默认为 0 (中国)
  });
  queryByLevelAndId(areaParams).then(response => {
    console.log(response);
    switch (level) {
      case 1:
        provinceList.value = response.data;
        break;
      case 2:
        cityList.value = response.data;
        break;
      case 3:
        districtList.value = response.data;
        break;
      default:
        break;
    }
  })
}
getAreaList(1, 0);
getList();
</script>
